package com.example.tests;


import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

public class TestBase {

	private static WebDriver driver;
	private static String baseUrl;

	@BeforeTest
	public void setUp() throws Exception {
	    driver = new FirefoxDriver();
	    baseUrl = "http://www.demosite.udozi.com";
	    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
	  }
	
	@AfterTest
	public void tearDown() throws Exception {
	    driver.quit();
	  }
	
	private boolean isElementPresent(WebDriver driver, By by){
		  try{
		   driver.findElement(by);
		   return true;
		  }catch(NoSuchElementException e){
		   return false;
		  }
		 }

	protected void verificationSuccessLogin() {
		if (isElementPresent(driver,By.linkText("Close"))) {
			driver.findElement(By.linkText("Close")).click();
			
		}
		
	    Assert.assertTrue(isElementPresent(By.id("ctl00_mdlBanner_btnMyAccount")));
	}

	protected void submitLoginInfo() {
		driver.findElement(By.cssSelector("#ctl00_mdlBanner_mdlLoginTooltip_btnLogin > span.rbText")).click();
	}

	protected void fillLoginForm(LoginData loginData) {
	    driver.findElement(By.id("ctl00_mdlBanner_mdlLoginTooltip_txtEmail")).sendKeys(loginData.email);
	    driver.findElement(By.id("ctl00_mdlBanner_mdlLoginTooltip_txtPassword")).sendKeys(loginData.password);
	}

	protected void openLoginForm() {
	    driver.findElement(By.cssSelector("#ctl00_mdlBanner_btnLogin > span.rbText.rbSecondary")).click();
	}

	protected void openMainPage() {
	    driver.get(baseUrl);
	}

	protected boolean isElementPresent(By selector) {
	    try {
	      driver.findElement(selector);
	      return true;
	    } catch (NoSuchElementException e) {
	      return false;
	    }
	  }

	public void verificationNonSuccessLogin() throws Exception { 
		  Thread.sleep(5000);
		  Assert.assertTrue(isElementPresent(By.id("ctl00_mdlBanner_btnLogin"))); 
		  Assert.assertEquals("The login/password hasn't being recognized.", driver.findElement(By.id("ctl00_mdlBanner_mdlLoginTooltip_txtEmail")).getAttribute("value"));
	  }

	protected void makeUserSuspended(String user) throws InterruptedException {
		Thread.sleep(1000);
		driver.findElement(By.id("ctl00_mdlBanner_btnMyAccount")).click();
		Thread.sleep(500);
		driver.findElement(By.cssSelector("div.footer")).click();
		Thread.sleep(1000);
		driver.findElement(By.xpath("//*[@id='ctl00_Main_mdlAdmin_rtsAdminTabs']//ul/li[3]//span[@class='rtsTxt']")).click(); 
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_txtFilterUsers")).clear();
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_txtFilterUsers")).sendKeys(user);
		Thread.sleep(1000);
		driver.findElement(By.cssSelector("#ctl00_Main_mdlAdmin_ctl00_rbFilterUsers > span.rbText")).click();
		Thread.sleep(1000);
		driver.findElement(By.cssSelector("#ctl00_Main_mdlAdmin_ctl00_rgUsers_ctl00_ctl04_rbEdit > span.rbText")).click();
		WebElement isChecked = driver.findElement(By.id("_rfdSkinnedctl00_Main_mdlAdmin_ctl00_rgUsers_ctl00_ctl05_EditFormControl_chkIsSuspended")); 
		if (!isChecked.isSelected()){
			isChecked.click();	
		}
		Thread.sleep(1000);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgUsers_ctl00_ctl05_EditFormControl_btnUpdateUser")).click(); 
	}

	protected void logOutFromSite() throws InterruptedException {
		driver.findElement(By.id("ctl00_mdlBanner_btnMyAccount")).click(); 
		Thread.sleep(2000);
		driver.findElement(By.id("ctl00_mdlBanner_mdlMyAccountTooltip_btnLogOut")).click();
		}

	protected void verificationSuspendedUserLogin() {
		Assert.assertTrue(isElementPresent(By.id("notifymessage"))); 
	    //Assert.assertTrue(driver.findElement(By.xpath("//div[@id = 'notifymessage']")).getText().contains("Your account has been suspended! Please contact vladislav.gunko@udozi.com to find out why and to reactivate your account.")); 
	}

	protected void verificationSignUp() {
		Assert.assertTrue(isElementPresent(By.id("RadToolTipWrapper_ctl00_mdlSplash_rttSplash")));
		driver.findElement(By.id("ctl00_mdlSplash_ctl01_btnClose")).click();
		Assert.assertTrue(isElementPresent(By.id("ctl00_mdlBanner_btnMyAccount")));
	}

	protected void submitSignUpInfo() {
		driver.findElement(By.id("ctl00_mdlBanner_mdlSignUpTooltip_btnSignUp")).click();
		
	}

	protected void openSingUpForm() {
		driver.findElement(By.id("ctl00_mdlBanner_btnSignUp")).click();	
		
	}

	protected void fillSignUpForm(SignUpData signUpCredentials) {
		driver.findElement(By.id("ctl00_mdlBanner_mdlSignUpTooltip_txtFullName")).sendKeys(signUpCredentials.fullName);
		driver.findElement(By.id("ctl00_mdlBanner_mdlSignUpTooltip_txtEmail")).sendKeys(signUpCredentials.email);
		driver.findElement(By.id("ctl00_mdlBanner_mdlSignUpTooltip_txtPassword")).sendKeys(signUpCredentials.password);
		//new Select(driver.findElement(By.id("ctl00_mdlBanner_mdlSignUpTooltip_cbHowDidYouHear_Input"))).selectByVisibleText(signUpCredentials.advertisement);		
		driver.findElement(By.id("ctl00_mdlBanner_mdlSignUpTooltip_cbHowDidYouHear_Input")).sendKeys(signUpCredentials.advertisement);
	}

	
	protected void verificationIncorrectSignUp() throws InterruptedException {
		  Thread.sleep(5000);
		  Assert.assertTrue(isElementPresent(By.id("RadToolTipWrapper_ctl00_mdlBanner_rttSignUp"))); 
		  Assert.assertEquals("This email has already been registered.", driver.findElement(By.id("ctl00_mdlBanner_mdlSignUpTooltip_txtEmail")).getAttribute("value"));
		
	}

	protected void navigateToStoreManagement() throws InterruptedException {
		driver.findElement(By.id("ctl00_mdlBanner_btnMyAccount")).click();
		//driver.findElement(By.id("ctl00_mdlBanner_mdlMyAccountTooltip_btnUserSite")).click();	
		Thread.sleep(500);
		driver.findElement(By.cssSelector("div.footer")).click();
	}

	protected void fillStoreForm(StoreData storeData){
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00_ctl03_ctl01_EditFormControl_txtStoreName")).sendKeys(storeData.storeName);
		driver.findElement(By.id("_rfdSkinnedctl00_Main_mdlAdmin_ctl00_rgStores_ctl00_ctl03_ctl01_EditFormControl_chkIsEditManually")).click();
		
		
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00_ctl03_ctl01_EditFormControl_txtAdminFirstName")).sendKeys(storeData.nameSA);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00_ctl03_ctl01_EditFormControl_txtAdminLastName")).sendKeys(storeData.lastnameSA);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00_ctl03_ctl01_EditFormControl_txtAdminTelephone")).sendKeys(storeData.phoneSA);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00_ctl03_ctl01_EditFormControl_txtAdminEmail")).sendKeys(storeData.emailSA);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00_ctl03_ctl01_EditFormControl_txtAdminPassword")).sendKeys(storeData.passwordSA);
				
	}

	protected void openNewStoreForm() {
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00_ctl03_ctl01_rbAddStore")).click();	
	}

	protected void verificationNewStore(StoreData storeData) throws InterruptedException {
		//String notification = "Store details of " + storeData.storeName + " has been successfully updated."; 
		Assert.assertTrue(isElementPresent(By.id("ctl00_RadNotification1_popup")));
		//Assert.assertTrue(driver.findElement(By.xpath("//*[@id='ctl00_RadNotification1_popup']//div[@id='notifymessage']")).getText().contains(notification));		
		searchStore(storeData);
	}

	private void searchStore(StoreData storeData) throws InterruptedException {
		searchStoreManagement(storeData);
		System.out.println(storeData.storeName);
		System.out.println(driver.findElement(By.xpath(".//*[@id='ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00']//tbody/tr//div[@class = 'store-title left']")).getAttribute("title"));
		Assert.assertEquals(storeData.storeName, driver.findElement(By.xpath(".//*[@id='ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00']//tbody/tr//div[@class = 'store-title left']")).getAttribute("title"));	
		
	}

	protected void searchStoreManagement(StoreData storeData)
			throws InterruptedException {
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_txtFilterStores")).sendKeys(storeData.storeName);
		Thread.sleep(500);
		driver.findElement(By.xpath("//*[@id='ctl00_Main_mdlAdmin_ctl00_rbFilterStores']/span")).click();
		Thread.sleep(10000);
	}

	protected void submitStoreInfo(){
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00_ctl03_ctl01_EditFormControl_btnInsertStore")).click();
	}

	protected void verificationStoreNotExist(StoreData storeData) throws InterruptedException {
		searchStoreManagement(storeData);
		System.out.println(driver.findElement(By.xpath("//*[@id='ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00']/tbody/tr//div[@class='list-empty']")).getText());
		Assert.assertEquals("There is no Stores to display.", driver.findElement(By.xpath("//*[@id='ctl00_Main_mdlAdmin_ctl00_rgStores_ctl00']/tbody/tr//div[@class='list-empty']")).getText());	
		
	}

	protected void verificationNewBranch(String storeName, BranchData branchData) {
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rcbStores_Input")).clear();
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rcbStores_Input")).sendKeys(storeName);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_btnFilterBranches")).click();
		Assert.assertEquals(branchData.branchName, driver.findElement(By.xpath("//*[@id='ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00']/tbody//div[@class= 'store-title left']")).getAttribute("title"));
	}

	protected void submitBranchInfo() {
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_EditFormControl_btnInsertBranch")).click();	
	}

	protected void fillBranchForm(BranchData branchData) {
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_EditFormControl_txtBranchName")).sendKeys(branchData.branchName);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_EditFormControl_txtStreet")).sendKeys(branchData.street);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_EditFormControl_txtCity")).sendKeys(branchData.city);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_EditFormControl_txtPostcode")).sendKeys(branchData.postCode);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_EditFormControl_txtBranchTelephone")).sendKeys(branchData.phone);
		
		driver.findElement(By.id("_rfdSkinnedctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_EditFormControl_chkShowPrice")).click();
		driver.findElement(By.id("_rfdSkinnedctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_EditFormControl_chkShowStock")).click();
		
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_EditFormControl_cctvpDefaultCategory_rcbDefaultCategory_Arrow")).click();
		
		
		driver.findElement(By.xpath("//*[@id='ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_EditFormControl_cctvpDefaultCategory_rcbDefaultCategory_i0_rtvDefaultCategories']//li[1]//span[3]")).click();
		
	}

	protected void openNewBranchForm() {
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00_ctl03_ctl01_rbAddStore")).click();
		
	}

	protected void verificationBranchNotExist(String storeName) {
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_rcbStores_Input")).sendKeys(storeName);
		driver.findElement(By.id("ctl00_Main_mdlAdmin_ctl00_btnFilterBranches")).click();
		Assert.assertEquals("rgNoRecords", driver.findElement(By.xpath("//*[@id='ctl00_Main_mdlAdmin_ctl00_rgBranches_ctl00']/tbody/tr")).getAttribute("class"));
	 
	}
	

}
